package com.yxhl.zoume.core.tripsmgmt.ui.fragment.home;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yxhl.protobuf.OrderBizType;
import com.yxhl.zoume.R;
import com.yxhl.zoume.common.ui.fragment.BaseFragment;
import com.yxhl.zoume.core.tripsmgmt.info.TicketCodeInfo;
import com.yxhl.zoume.core.tripsmgmt.info.ZMPassenger;
import com.yxhl.zoume.core.tripsmgmt.presenter.zoumebusticket.ZouMeBusTicketPresenter;
import com.yxhl.zoume.core.tripsmgmt.ui.activity.TicketCodeActivity;
import com.yxhl.zoume.core.tripsmgmt.ui.adapter.TicketQRAdapter;
import com.yxhl.zoume.core.tripsmgmt.view.zoumebusticket.ZouMeBusTicketView;
import com.yxhl.zoume.di.component.trip.TicketQRComponent;
import com.yxhl.zoume.utils.CollectionUtils;
import com.yxhl.zoume.utils.LOG;
import com.yxhl.zoume.utils.PromptUtils;
import com.yxhl.zoume.utils.StringUtils;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by vhawk on 15/9/30.
 * 车票展示页面
 * 第三期
 */
public class TicketQRCodeFragment extends BaseFragment implements ZouMeBusTicketView {

    private static final String TAG = "TicketQRCodeFragment";
    private static final String KEY_TICKET_PARAM = "TICKET_PARAM";
    private static final String KEY_ORDER_ID = "ORDER_ID";
    private static final String KEY_ORDER_BIZ_TYPE = "key_order_biz_type";


    @BindView(R.id.view_pager_ticket)
    ViewPager ticketCodeViewPager;
    @BindView(R.id.tv_include_trip_detail_station_info_start)
    TextView startStationTv;
    @BindView(R.id.tv_include_trip_detail_station_info_destination)
    TextView arriveStationTv;
    @BindView(R.id.tv_include_trip_detail_station_info_date)
    TextView departDateTv;
    @BindView(R.id.tv_include_trip_detail_station_info_clock)
    TextView departTimeTv;
    @BindView(R.id.iv_include_trip_detail_station_info_icon)
    ImageView stationInfoIconIv;
    @BindView(R.id.tv_passenger_name)
    TextView passengerNameTv;
    @BindView(R.id.tv_passenger_mobile)
    TextView passengerMobileTv;
    @BindView(R.id.tv_passenger_id_num)
    TextView passengerIdNumTv;
    @BindView(R.id.tv_pager_index)
    TextView passengerPageIndexTv;

    @BindView(R.id.iv_previous_ticket)
    ImageView previousTicketIv;
    @BindView(R.id.iv_next_ticket)
    ImageView nextTicketIv;

    @Inject
    ZouMeBusTicketPresenter presenter;
    private TicketQRComponent component;
    private int passengerNumber;
    private TicketCodeInfo ticketCodeInfo;
    private OrderBizType bizType;
    private String orderId;

    public static TicketQRCodeFragment newInstance(String orderId, OrderBizType bizType) {
        TicketQRCodeFragment ticketCodeFragment = new TicketQRCodeFragment();
        Bundle bundle = new Bundle();
        bundle.putCharSequence(KEY_ORDER_ID, orderId);
        bundle.putSerializable(KEY_ORDER_BIZ_TYPE, bizType);
        ticketCodeFragment.setArguments(bundle);
        return ticketCodeFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handleArguments();
        presenter.attachView(this);
        presenter.getZouMeBusTicketList(component.context(), orderId);
    }

    private void handleArguments() {
        Bundle arguments = getArguments();
        if (arguments != null) {
            orderId = arguments.getString(KEY_ORDER_ID);
            bizType = (OrderBizType) arguments.getSerializable(KEY_ORDER_BIZ_TYPE);
        }
    }

    /**
     * 初始化，车票二维码
     */
    private void initQrCode(List<String> testTicketCodeList) {
        TicketQRAdapter qrAdapter = new TicketQRAdapter(component.context(), testTicketCodeList);
        ticketCodeViewPager.setAdapter(qrAdapter);
        ticketCodeViewPager.setCurrentItem(0);
        ticketCodeViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                flushPassengerDate(ticketCodeInfo.getPassengerList().get(position));
                passengerPageIndexTv.setText(position + 1 + "/" + passengerNumber);
                if (passengerNumber == 2) {
                    switch (position) {
                        case 0:
                            previousTicketIv.setVisibility(View.INVISIBLE);
                            nextTicketIv.setVisibility(View.VISIBLE);
                            break;
                        case 1:
                            previousTicketIv.setVisibility(View.VISIBLE);
                            nextTicketIv.setVisibility(View.INVISIBLE);
                            break;
                    }
                } else {
                    if (position == 0) {
                        previousTicketIv.setVisibility(View.INVISIBLE);
                        nextTicketIv.setVisibility(View.VISIBLE);
                    } else if (position == 1) {
                        previousTicketIv.setVisibility(View.VISIBLE);
                        nextTicketIv.setVisibility(View.VISIBLE);
                    } else if (position == passengerNumber - 1) {
                        previousTicketIv.setVisibility(View.VISIBLE);
                        nextTicketIv.setVisibility(View.INVISIBLE);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void flushPassengerDate(ZMPassenger zmPassenger) {
        passengerNameTv.setText(zmPassenger.getName());
        passengerMobileTv.setText(zmPassenger.getMobile());
        passengerIdNumTv.setText(StringUtils.getFilteredAccountName(zmPassenger.getIdNumber()));

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        if (bizType != null) {
            switch (bizType) {
                case TICKET:
                    stationInfoIconIv.setImageResource(R.drawable.ic_bashi_002);
                    break;
                case ORDER_BIZ_SHARE:
                case ORDER_BIZ_CHARTER:
                    stationInfoIconIv.setImageResource(R.drawable.ic_kuaiche);
                    break;
                case OBT_DIY_BUS:
                    stationInfoIconIv.setImageResource(R.drawable.ic_dingzhibashibiaoqian);
                    break;
                case OBT_FT_BUS:
                    stationInfoIconIv.setImageResource(R.drawable.ic_bashibiaoqian);
                    break;
            }
        }
        return view;
    }

    /**
     * 页面初始化展示
     */
    private void flashUI() {
        startStationTv.setText(ticketCodeInfo.getStartStation());
        arriveStationTv.setText(ticketCodeInfo.getArriveStation());
        departDateTv.setText(ticketCodeInfo.getDepartDate());
        departTimeTv.setText(ticketCodeInfo.getDepartTime());
    }

    @OnClick(R.id.iv_next_ticket)
    public void nextTicketClick() {
        int currentItemIndex = ticketCodeViewPager.getCurrentItem();
        if (passengerNumber >= 2) {
            if (currentItemIndex <= passengerNumber - 2) {
                currentItemIndex++;
                ticketCodeViewPager.setCurrentItem(currentItemIndex);
            }
        }

        LOG.v(TAG, "nextTicketClick");
        LOG.v(TAG, "currentItemIndex = " + currentItemIndex);
    }

    @OnClick(R.id.iv_previous_ticket)
    public void previousTicketClick() {
        int currentItemIndex = ticketCodeViewPager.getCurrentItem();
        if (passengerNumber >= 2) {
            if (currentItemIndex >= 1) {
                currentItemIndex--;
                ticketCodeViewPager.setCurrentItem(currentItemIndex);
            }
        }
        LOG.v(TAG, "previousTicketClick");
        LOG.v(TAG, "currentItemIndex = " + currentItemIndex);
    }

    @Override
    public void handleZouMeBusTicketSucceed(TicketCodeInfo info) {
        ticketCodeInfo = info;
        initQrCode(info.getTicketCodeList());
        flashUI();
        if (!CollectionUtils.isEmpty(info.getPassengerList())) {
            passengerNumber = info.getPassengerList().size();
            if (passengerNumber == 1) {
                previousTicketIv.setVisibility(View.GONE);
                nextTicketIv.setVisibility(View.GONE);
            }
            flushPassengerDate(info.getPassengerList().get(0));
            passengerPageIndexTv.setText("1/" + passengerNumber);
        }
    }

    @Override
    public void handleZouMeBusTicketFailure(String msg) {
        PromptUtils.showShort(component.context(), msg);
    }

    @Override
    public void handleZouMeBusTicketNetError() {
        PromptUtils.showShort(component.context(), R.string.net_error);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


    @Override
    protected void initializeInjector() {
        component = getComponent(TicketQRComponent.class);
        component.inject(this);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_trip_mgmt_reservation_ticket;
    }

    @Override
    protected void setupToolBar() {
        ((TicketCodeActivity) mActivity).setToolbar(getString(R.string.my_ticket), R.drawable.ic_fuwushuoming);
    }


}
